const Mock = require('mockjs')
const Random = Mock.Random
const List = []

Random.extend({
  constellation: function(date) {
    var constellations = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座']
    return this.pick(constellations)
  },
  gender_ch: function(boolean) {
    var genders = ['男', '女']
    return this.pick(genders)
  },
  major: function() {
    var majors = ['计算机科学与技术', '软件工程', '临床医学', '生化环材', '临床医学', '临床药学']
    return this.pick(majors)
  },
  org: function() {
    var majors = ['计算机服务队', '软件俱乐部', '自行车协会', '红会', '学生会']
    return this.pick(majors)
  },
  position: function() {
    var poss = ['社长', '部长', '干事']
    return this.pick(poss)
  },
  status: function() {
    var statuss = ['在职', '离队', '退役']
    return this.pick(statuss)
  }
})

for (let i = 0; i < 100; i++) {
  List.push(Mock.mock({
    id: i + 1,
    gender: '@Gender_ch',
    name: '@first',
    password: '@string',
    class: '@Major' + '@string(number,3)',
    org: '@Org',
    position: '@Position',
    status: '@Status',
    intro: '@string(300)',
    img: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F-Po3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fbd315c6034a85edf1ef4cc334a540923dd5475a1.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620506668&t=43008dd2f5e6b58ec562397499ed5c71'
  }))
}

module.exports = [
  {
    url: '/org-manage-system/member/list',
    type: 'get',
    response: config => {
      const { type, title, page, limit, onlyWorking } = config.query
      console.log(title)
      const mockList = List
      const status = onlyWorking
      console.log(status)
      const listAfterFilter = mockList.filter(
        (item, index) => {
          var flag = true
          let statusFlag = true
          if (title != null && title !== '') {
            flag = false
            if (type != null && type !== '') {
              flag = item[type].search(title) >= 0
            } else {
              flag = item.name.search(title) >= 0 || item.org.search(title) >= 0 || item.class.search(title) >= 0 || item.gender.search(title) >= 0 || item.position.search(title) >= 0
            }
          }
          if (status === 'true') {
            statusFlag = item.status.search('在职') >= 0
          }
          return flag && statusFlag
        }
      )
      const pageList = listAfterFilter.filter(
        (item, index) => {
          return index < limit * page && index >= limit * (page - 1)
        })
      return {
        status: 200,
        data: {
          total: listAfterFilter.length,
          items: pageList
        }
      }
    }
  },
  {
    url: '/org-manage-system/member/detail',
    type: 'get',
    response: config => {
      const { id } = config.query
      var mockList = List
      return {
        status: 200,
        data: {
          items: mockList[id - 1]
        }
      }
    }
  },
  {
    url: '/org-manage-system/member/names',
    type: 'get',
    response: config => {
      const { page, limit, vague } = config.query
      let names = config.query.names
      if (typeof names === 'string') {
        const temp = names
        names = []
        names.push(temp)
      }
      let mockList = List
      console.log(names)
      mockList = mockList.filter(
        (item, index) => {
          for (const i in names) {
            if (vague && item.name.search(names[i]) >= 0) {
              return true
            } else if (item.name === names[i]) {
              return true
            }
          }
          return false
        }
      )
      const pageList = mockList.filter(
        (item, index) => {
          return index < limit * page && index >= limit * (page - 1)
        }
      )
      return {
        status: 200,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  }
]
